<!-- 创建时间: 2022年01月01日 星期六 13:55 -->
<template>
    <div class='App_container'>
        <el-row type="flex">
            <el-col :xs="24" :sm="6" :md="4" :lg="3" :xl="2" style="margin-right: 2px;" v-for="(item, index) in commodityData" :key="index">
                <el-card class="box-card">
                    <div class="cardBody">
                        <el-image style="width: 100px; height: 100px" :src="item.commodityImg" fit="fill"></el-image>
                        <span>{{item.commodityName}}&nbsp;&nbsp;<b>¥{{item.commodityPrice}}</b> </span>
                        <el-input-number size="mini" @change="calcCommNumber(item)" v-model="item.commodityNumber" :precision="0" :min="0" :max="9999"></el-input-number>
                    </div>
                </el-card>
            </el-col>
        </el-row>
        <el-row type="flex">
            <el-col :xs="12" :sm="4" :md="3" :lg="2" :xl="1" class="payInfo">
                <span class="totalPrice"> ¥{{totalPrice}} </span>
            </el-col>
            <el-col :xs="12" :sm="4" :md="3" :lg="2" :xl="1" class="payInfo">
                <!-- 父组件必需元素 -->
                <el-button type="primary" :disabled="totalPrice==0" @click="confirmPay">确认支付</el-button>
            </el-col>
        </el-row>
        <hr>
        <!-- 父组件必需元素 -->
        <paymentCreate :selectCommodity="selectCommodity" ref="child"></paymentCreate>
    </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import paymentCreate from './components/paymentCreate.vue'

export default {
    //import引入的组件需要注入到对象中才能使用
    components: {
        paymentCreate
    },
    data() {
        //这里存放数据
        return {
            //测试数据
            commodityData: [
                { commodityId: '000001', commodityName: '牛肉汉堡', commodityNumber: 1, commodityPrice: 12, commodityImg: 'https://tse1-mm.cn.bing.net/th/id/OIP-C.88Lusj1nsgz1BDYd7yHBzAHaE8?w=251&h=180&c=7&r=0&o=5&dpr=2&pid=1.7' },
                { commodityId: '000002', commodityName: '鸡肉汉堡', commodityNumber: 0, commodityPrice: 10, commodityImg: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.XoqYFq0_q14mZ-hCM5YZtQHaFj?w=245&h=184&c=7&r=0&o=5&dpr=2&pid=1.7' }
            ],
            // 传子组件值--必需
            selectCommodity: [],
            totalPrice: 0,
        };
    },
    //监听属性 类似于data概念
    computed: {},
    //监控data中的数据变化
    watch: {},
    //方法集合
    methods: {
        confirmPay(){
            this.$refs.child.confirmPay();
        },
        calcCommNumber(e) {
            let data = {
                id: e.commodityId,
                number: e.commodityNumber,
                price: e.commodityPrice
            };
            let sc = this.selectCommodity,
                totalPrice = 0;
            if (sc.length == 0) {
                sc.push(data);
                this.totalPrice = data.price;
                return;
            }
            for (let i = 0; i < sc.length; i++) {
                if (sc[i].id == e.commodityId) {
                    sc[i].number = e.commodityNumber
                }
                totalPrice += sc[i].number * sc[i].price
            }
            if (this.totalPrice == totalPrice) {
                sc.push(data);
                totalPrice += data.number * data.price
            }
            this.totalPrice = totalPrice;
        }
    },
    //生命周期 - 创建完成（可以访问当前this实例）
    created() {
        this.calcCommNumber(this.commodityData[0])
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {
        
    },
    //当data中定义的数据有变化时就会加载updated方法
    updated() {},
};
</script>

<style lang='scss' scoped>
//@import url(); 引入公共css类
.App_container {
    .box-card .cardBody ,.payInfo{
        display: flex;
        align-items: center;
        flex-direction: column;
        justify-content: space-evenly;
    }
    .row-bg,
    .totalPrice {
        text-align: center;
        font-size: 33px;
    }
}
</style>